export default {
    namespaced: true,
    state: {//仓库
        quantity:1,//数量
        idx:null, /*----用户当前在看那个详情页商品---*/
        data:[]//存储所有详情页已经加载的数据
    },
    //计算
    getters: {
        list:({ data, idx }, getters) => {//存储商品信息 //是对象
            let target = data.find(item => item.id === idx);
            return target || null;
        }
    },
    //更改仓库
    mutations: {
        _addDetail(state, payload){
            state.data.push(payload);
        },
        _toggleIdx(state,payload){
            state.idx = payload;
        }
    },
    //异步请求
    actions: {
        getProduct({ state: {data} ,commit, rootState: {$http}}, payload){//rootState总仓库的$http解构
            commit("_toggleIdx",payload);
            let isExist = data.find(item => item.id === payload);
            if(!isExist) {
                $http.post("/detail", {id: payload})//路由和传参
                    .then(result => commit("_addDetail", result));
            }
        }
    }
};